home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
timedat.exe
/
TIMEDATE.DOC
< prev
next >
Wrap
Text File
|
1992-05-09
|
4KB
|
116 lines
TIME...
I've been teaching my self C++. And while all the standard
texts keep rambling on about polymorphism, inheritance, virtual
functions and the like, I've gotten the most benefits from
simple encapsulation and operator overloading.
They reduce my repeated and visible code, and this helps reduce
errors, reduce production time, and reduce repair time.
------------------------------------------------------------
If you had to compare two timedate pairs to see if they were
equal, which method would you choose?
This one...
if ( (td1.year==td2.year) &&
(td1.month==td2.month) &&
(td1.day==td2.day) &&
(td1.hour==td2.hour) &&
(td1.min==td2.min) &&
(td1.sec==td2.sec) )
printf("Equal\n");
Or this one...
if (td1==td2)
printf("Equal\n");
------------------------------------------------------------
If you had to compare two timedate pairs to verify that a
process starting time entered by a user is before the
process ending time entered by a user, which method would
you choose?
This one...
if ( (td1.year<td2.year) ||
((td1.year==td2.year) && (td1.month<td2.month)) ||
((td1.year==td2.year)&&(td1.month==td2.month)&&(td1.day<td2.day)) ||
((td1.year==td2.year)&&(td1.month==td2.month)&&(td1.day==td2.day)&&
(td1.hour<td2.hour)) ||
((td1.year==td2.year)&&(td1.month==td2.month)&&(td1.day==td2.day)&&
(td1.hour==td2.hour)&&(td1.min<td2.min)) ||
((td1.year==td2.year)&&(td1.month==td2.month)&&(td1.day==td2.day)&&
(td1.hour==td2.hour)&&(td1.min==td2.min)&&(td1.sec<td2.sec)) )
printf("Good Times\n");
else
printf("Bad Times\n");
Or this one...
if (td_begin<td_end) printf("Good Times\n");
else printf("Bad Times\n");
------------------------------------------------------------
It became obvious after a little examination that one class
would not do the job, because we are dealing with two
fundamentally different concepts.
Points IN Time and Spans OF Time
Some operations that can be performed on one type of object,
make no sense when applied to the other type of object.
If a project consists of two processes "A" and "B".
And the time to complete phase "A" is 2 days and the time
to complete phase "B" is 3 days. Then the time to complete
the entire project is 2days + 3days = 5days.
But, following the same operation, you can't add two POINTS
in time together. (You can't add 1492 when Columbus discovered
the new world to July 4th, 1776 and get the war of 1812. Adding
POINTS in time is a senseless operation.)
On the other hand, SPANS of time CAN be added to a POINT in time.
(If today is May 6th, 1992, then 2 days from now is
5/6/92 + 2days = 5/8/92. So a POINT plus a SPAN equals another
later POINT in time.)
There are several combinations and several exclusions, you'll
just have to read through the operator overloading list
and the demo, and think about what operations are sensible.
------------------------------------------------------------
DEMO.CPP tests and demonstrates all the operator overloading
possibilities in the timepoint and timespan classes.
TIMPOINT.CPP is a class for defining and manipulating
"Points IN Time"
TIMPOINT.H is a declaration file for TIMPOINT.CPP
TIMESPAN.CPP is a class for defining and manipulating
"Spans OF Time"
TIMESPAN.H is a declaration file for TIMESPAN.CPP
You get what you pay for, and this code for nothing, so, there
are no guarantees. It works for me but, it's up to you to use
the code enclosed responsibly and correctly.
I offer the code to the public domain for all to use with no
royalties or costs. Pass it on to anyone that wants it.
If you have any suggestions or comments please E-Mail me at
John K. Humkey
CompuServe: [73270,3166]
INTERNET: 73270.3166@compuserve.com
GOOD LUCK!